<template>
	<view class="page-container back-grey-light pcol-lg" style="padding-bottom: 136rpx;">
		<view v-if="list.length" class="">
			<view v-for="(item, index) in list" :key="index" class="radius-l back-white mrow-l mb-l">
				<view class="row-box prow-l pcol-base" @tap.stop="selectAddress(item)">
					<image v-if="item.status" class="icon30" src="../static/icon_danxuan_light.png" @tap.stop="setStatus(index, 0)"></image>
					<image v-else class="icon30" src="../static/icon_danxuan.png" @tap.stop="setStatus(index, 1)"></image>
					<view class="ml-base col-box">
						<view class="row-box">
							<text>{{ item.name }}</text>
							<text class="s26 ml-l grey-light">{{ item.tel }}</text>
						</view>
						<text class="s24">{{ item.schoolName+item.detailAdd }}</text>
					</view>
				</view>
				<view class="row-box right" style="border-top: 1rpx solid #f8f8f8;">
					<u-icon class="pall-l" name="edit-pen" :color="nomarlColor" :size="26"
						label='编辑' label-size="26" label-pos="right" :label-color="nomarlColor" margin-left="10" @tap.stop="goEdit(item)">
					</u-icon>
					<u-icon class="pall-l" style="margin-left: 40rpx;" name="trash" :color="nomarlColor" :size="26"
						label='删除' label-size="26" label-pos="right" :label-color="nomarlColor" margin-left="10" @tap.stop="remove(item.id)">
					</u-icon>
				</view>
			</view>
			<u-loadmore :status="loadStatus" />
		</view>
		
		<view v-else class="col-box center pcol-lg">
			<image src="/static/icon/nodata.png" style="width: 400rpx;height: 400rpx;"></image>
			<text class="s24 grey-light">还没有保存的地址~</text>
		</view>
		
		<!-- 提交按钮 -->
		<view class="submit-button-box back-white" style="padding: 24rpx 0;">
			<view class="submit-button back-primary" @tap="goEdit()">新增</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageSize: 10,
				pageIndex: 1,
				list: [],
				loadStatus: 'loadmore', // 加载状态
			};
		},
		onShow() {
			this.getList(0)
		},
		onPullDownRefresh() {
			this.getList(0)
		},
		onReachBottom() {
			this.getList(1)
		},
		methods: {
			// ----------- 用户操作
			// 跳转编辑地址
			goEdit(item=null) {
				if (item) {
					uni.navigateTo({
						url: `/pages/subPackage/user/my_address_edit/my_address_edit?address=${encodeURIComponent(JSON.stringify(item))}`
					})
				} else {
					uni.navigateTo({
						url: `/pages/subPackage/user/my_address_edit/my_address_edit`
					})
				}
			},
			// 删除地址
			remove(id) {
				uni.showModal({
					content: '确定删除该地址？',
					confirmColor: this.primaryColor,
					success: (res) => {
						if (res.confirm) {
							this.$api.DelMyAddress({
								ids: [id]
							}).then(res => {
								console.log('DelMyAddress-success', res)
								if (res.data.code == 200) {
									uni.startPullDownRefresh({
										success: () => {
											uni.showToast({
												icon: 'none',
												title: '删除成功！'
											})
										}
									})
								} else {
									uni.showToast({
										icon: 'none',
										title: res.data.msg
									})
								}
							}).catch(err => {
								console.log('DelMyAddress-fail', err)
								
							})
						}
					}
				})
			},
			// 将地址设为默认
			setStatus(index, status) {
				let item = this.list[index]
				this.$api.UpdateMyAddressStatus({
					id: item.id
				}).then(res => {
					console.log('UpdateMyAddressStatus-success', res)
					if (res.data.code == 200) {
						this.list.forEach((item, index2) => {
							if (status == 1) item.status = 0
							if (index == index2) {
								item.status = status
							}
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: res.data.msg
						})
					}
				}).catch(err => {
					console.log('UpdateMyAddressStatus-fail', err)
					
				})
			},
			// 选择地址
			selectAddress(item) {
				let pages = getCurrentPages()
				let prePage = pages[pages.length-2]
				console.log(prePage)
				if (prePage.route=='pages/subPackage/merchant/order_submit/order_submit') {
					prePage.$vm.order.addressOrderInfoVo = this.deepClone(item)
					uni.navigateBack()
				}
			},
			// ----------- 
			
			// ----------- 获取数据
			// 获取收获地址
			getList(condition) {
				if (condition==0) {
					this.pageIndex = 1
				} else if (this.pageIndex >= this.pageCount) {
					return this.loadStatus = 'nomore'
				} else {
					this.pageIndex += 1
				}
				this.loadStatus = 'loading'
				this.$api.GetMyAddressList({
					current: this.pageIndex,
					limit: this.pageSize
				}).then(res => {
					console.log('getList-success', res)
					if (res.data.code == 200) {
						if (this.pageIndex == 1) {
							this.list = res.data.data.records
							this.pageCount = res.data.data.pages
						} else {
							this.list = [...this.list, ...res.data.data.records]
						}
						if (this.pageIndex < this.pageCount) {
							this.loadStatus = 'loadmore'
						} else {
							this.loadStatus = 'nomore'
						}
					} else {
						uni.showToast({
							icon: 'none',
							title: res.data.msg
						})
					}
				}).catch(err => {
					console.log('getList-fail', err)
					
				}).finally(() => {
					uni.stopPullDownRefresh()
				})
			},
			// ----------- 
		},
	}
</script>

<style lang="scss">
	// 按钮
	.submit-button-box {
		width: 100%;
		position: fixed;
		bottom: 0;
		z-index: 99;
	}
</style>
